package com.ruoyi.web.controller.netty.demo;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

//适配器类，用于处理具体的事件
@Slf4j
@ChannelHandler.Sharable
public class NettyServerChannelHandler extends ChannelInboundHandlerAdapter {
    /**
     * 服务端读取数据
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        log.info("接收到的数据为:" + byteBuf.toString(CharsetUtil.UTF_8));
        super.channelRead(ctx, msg);
        ctx.writeAndFlush(msg);
        //短连接,直接关闭
        ctx.close();
    }

}
